﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using QASampling.BusinessObject;
using QASampling.Util;
using QASampling.UI;

namespace QASampling.QAControl
{
    public partial class QACheckBox : CheckBox
    {
        private ToolTip tooltip;
        private string errorMsg;

        private string originalValue = "";
        public string OriginalValue
        {
            get { return originalValue; }
            set { originalValue = value; SetBackgroundColor(); }
        }

        private JenisField fieldType;
        public JenisField FieldType
        {
            get { return fieldType; }
            set { fieldType = value; }
        }

        private bool readOnly;
        public bool ReadOnly
        {
            get { return readOnly; }
            set { readOnly = value; }
        }

        private string fieldRules;
        public string FieldRules
        {
            get { return fieldRules; }
            set { fieldRules = value; }
        }

        private BaseFormSPTWithHeader form;
        public BaseFormSPTWithHeader Form
        {
            get { return form; }
            set { form = value; }
        }

        public QACheckBox()
        {
            InitializeComponent();
            tooltip = new ToolTip();
            this.tooltip.AutoPopDelay = 1000;
            this.tooltip.InitialDelay = 1000;
            this.tooltip.ReshowDelay = 500;
            this.GotFocus += new EventHandler(QACheckBox_GotFocus);
            this.LostFocus += new EventHandler(QACheckBox_LostFocus);
        }

        void QACheckBox_LostFocus(object sender, EventArgs e)
        {
            tooltip.Hide(this);
        }

        void QACheckBox_GotFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(errorMsg))
            {
                tooltip.Show(string.Empty, this, 0);
                tooltip.SetToolTip(this, errorMsg);
                tooltip.Show(errorMsg, this, Width - 40, Height);
            }            
        }

        protected override void OnClick(EventArgs e)
        {
            // pass the event up only if its not readlonly
            if (!ReadOnly) base.OnClick(e);
        }

        private void QACheckBox_Validating(object sender, CancelEventArgs e)
        {
            errorMsg = string.Empty;
            if (this.FindForm() != null)
            {
                if (!ValidationUtil.CalculateFieldsMathExp(this, this.fieldRules, ref errorMsg))
                {
                    BackColor = Color.Red;
                    QAFieldErrorMsg fem = new QAFieldErrorMsg();
                    //fem.DocId = QAController.DocId;
                    fem.Field = this.Text;
                    fem.ErrorMsg = errorMsg;
                    if (QAController.FieldErrorMsg.IndexOf(fem) < 0)
                        QAController.FieldErrorMsg.Add(fem);
                    tooltip.Show(string.Empty, this, 0);
                    tooltip.SetToolTip(this, errorMsg);
                    tooltip.Show(errorMsg, this, Width - 40, Height);
                }
                else
                {
                    errorMsg = string.Empty;
                    SetBackgroundColor();
                    tooltip.Hide(this);                    
                }
            }
        }

        private void SetBackgroundColor()
        {
            bool b = this.originalValue == "1" ? true : false;
            if (b != this.Checked)
                BackColor = Color.LightGreen;
            else if (!string.IsNullOrEmpty(errorMsg))
                BackColor = Color.Red;
            else
                BackColor = Color.White;
        }

        private void QACheckBox_MouseHover(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(errorMsg))
            {
                tooltip.Show(string.Empty, this, 0);
                tooltip.SetToolTip(this, errorMsg);
                tooltip.Show(errorMsg, this, 0, Height);
            }
        }

        private void QACheckBox_MouseLeave(object sender, EventArgs e)
        {
            tooltip.Hide(this);
        }

        private void QACheckBox_CheckedChanged(object sender, EventArgs e)
        {
            SetBackgroundColor();
        }
    }
}
